package com.example.function.project.controller;

import com.example.common.page.CP_Page;
import com.example.common.page.CP_Sorter;
import com.example.common.support.*;
import com.example.function.project.model.one.Project;
import com.example.function.project.model.pojo.ProjectEditPojo;
import com.example.function.project.model.pojo.ProjectPagePojo;
import com.example.function.project.requestBody.ProjectPageRequestBody;
import com.example.function.project.requestBody.ProjectRequestBody;
import com.example.function.project.requestBody.ProjectSaveRequestBody;
import com.example.function.project.requestBody.ProjectUserRequestBody;
import com.example.function.project.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Description: <project模块controller >. <br>
 * <p>
 * <基本的crud>
 * </p>
 * generate time:2021-7-27 14:47:24
 *
 * @author hanqf
 * @version V1.0
 */

@RestController
@Slf4j
@RequestMapping("/proj")
public class ProjectController {


    @Resource(name = "projectService")
    protected IProjectService projectService;


    /**
     * 查询全部.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/projects")
    @CP_OperateLog(value = "查询全部项目", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projects() {
        final List<Project> projectList = projectService.findAll();
        return AjaxResponse.success(projectList);
    }


    /**
     * 分页查询.
     *
     * @return AjaxResponse
     */
    @PostMapping(value = "/projects/page")
    @CP_OperateLog(value = "分页查询项目", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projectsPage(@RequestBody(required = false) ProjectPageRequestBody requestBody) {

        ProjectRequestBody projectRequestBody;
        CP_Page pageInfo;
        if (requestBody != null) {
            projectRequestBody = requestBody.getProject() == null ? new ProjectRequestBody() : requestBody.getProject();
            pageInfo = requestBody.getPageInfo() == null ? new CP_Page() : requestBody.getPageInfo();
        } else {
            projectRequestBody = new ProjectRequestBody();
            pageInfo = new CP_Page();
        }

        final Page<Project> resultPage = projectService.findByPage(pageInfo, new CP_Sorter(), projectRequestBody);
        pageInfo.setTotal(resultPage.getTotalElements());

        List<ProjectPagePojo> pojoList = new ArrayList<>();

        List<Project> projectsList = resultPage.getContent();
        for (Project project : projectsList) {
            final ProjectPagePojo projectPagePojo = ProjectPagePojo.wrapper(project);
            pojoList.add(projectPagePojo);
        }

        return AjaxResponse.success(new CP_BasePageResponseBody(pojoList, pageInfo));
    }


    /**
     * 新增项目
     *
     * @return AjaxResponse
     */
    @PostMapping(value = "/project")
    @CP_OperateLog(value = "新增项目", type = CP_GlobalNamingConstant.OPERATE_ADD)
    public AjaxResponse projectAdd(@RequestBody ProjectSaveRequestBody requestBody) {

        ProjectRequestBody projectBody = requestBody.getProject();
        List<ProjectUserRequestBody> projectUsersBody = requestBody.getProjectUsers();
        if (projectBody == null || projectUsersBody == null) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "请求JSON格式错误");
        }

        Project project = projectService.addProjectAndUser(projectBody, projectUsersBody);
        return AjaxResponse.success(project);
    }

    /**
     * 更新项目
     *
     * @return AjaxResponse
     */
    @PutMapping(value = "/project")
    @CP_OperateLog(value = "更新项目", type = CP_GlobalNamingConstant.OPERATE_DETAIL_MODIFY)
    public AjaxResponse projectUpdate(@RequestBody ProjectSaveRequestBody requestBody) {

        ProjectRequestBody projectBody = requestBody.getProject();
        List<ProjectUserRequestBody> projectUsersBody = requestBody.getProjectUsers();
        if (projectBody == null || projectUsersBody == null) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "请求JSON格式错误");
        }

        Project project = projectService.updateProjectAndUser(projectBody, projectUsersBody);
        return AjaxResponse.success(project);
    }


    /**
     * 按项目id查询项目.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/project/{projectId}")
    @CP_OperateLog(value = "按项目id查询项目", type = CP_GlobalNamingConstant.OPERATE_EDIT)
    public AjaxResponse projectById(@PathVariable("projectId") Long projectId) {
        Project project = projectService.findById(projectId);
        if (project == null) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "查询的项目不存在");
        }
        return AjaxResponse.success(ProjectEditPojo.wrapper(project));
    }


}

